package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PCityBo;
import com.ruoyi.system.domain.vo.PCityVo;
import com.ruoyi.system.domain.PCity;
import com.ruoyi.system.mapper.PCityMapper;
import com.ruoyi.system.service.IPCityService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 城市Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-10
 */
@RequiredArgsConstructor
@Service
public class PCityServiceImpl implements IPCityService {

    private final PCityMapper baseMapper;

    /**
     * 查询城市
     */
    @Override
    public PCityVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询城市列表
     */
    @Override
    public List<PCityVo> queryList(PCityBo bo) {
        LambdaQueryWrapper<PCity> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PCity> buildQueryWrapper(PCityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PCity> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCityId() != null, PCity::getCityId, bo.getCityId());
        lqw.eq(bo.getLevel() != null, PCity::getLevel, bo.getLevel());
        lqw.eq(bo.getParentId() != null, PCity::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getAreaCode()), PCity::getAreaCode, bo.getAreaCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), PCity::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getMergerName()), PCity::getMergerName, bo.getMergerName());
        lqw.eq(StringUtils.isNotBlank(bo.getLng()), PCity::getLng, bo.getLng());
        lqw.eq(StringUtils.isNotBlank(bo.getLat()), PCity::getLat, bo.getLat());
        lqw.eq(bo.getIsShow() != null, PCity::getIsShow, bo.getIsShow());
        return lqw;
    }

    /**
     * 新增城市
     */
    @Override
    public Boolean insertByBo(PCityBo bo) {
        PCity add = BeanUtil.toBean(bo, PCity.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改城市
     */
    @Override
    public Boolean updateByBo(PCityBo bo) {
        PCity update = BeanUtil.toBean(bo, PCity.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PCity entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除城市
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
